package win.demonlegion.controller;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import win.demonlegion.common.HttpCode;
import win.demonlegion.thirdparty.module.JSWechatConfig;
import win.demonlegion.thirdparty.service.WechatService;
import win.demonlegion.util.ResultUtil;

@RestController
@CrossOrigin
@RequestMapping("wechat/")
public class WechatController {
    private static final Logger logger = LoggerFactory.getLogger(WechatController.class);

    @Autowired
    private WechatService wechatService;

    @RequestMapping("js/config")
    public String getJSConfig(String url) {
        if(StringUtils.isNotEmpty(url)) {
            JSWechatConfig jsWechatConfig = wechatService.getJSWechatConfig(url);
            if (jsWechatConfig != null) {
                logger.debug(JSON.toJSONString(jsWechatConfig));
                return ResultUtil.getResult(HttpCode.SUCCESS, "jsWechatConfig", jsWechatConfig);
            }
        }
        return ResultUtil.getResult(HttpCode.FAIL);
    }
}
